﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DAL.Service.Impl
{
    public class InfomationPublishServiceImpl : BaseServiceImpl, IInfomationPublishService
    {
        public IList getlist(int pageno, int pagesize, int template)
        { 
            MyQuery query = new MyQuery();
            StringBuilder sb = new StringBuilder();
            if (pageno == 1)
            {
                sb.Append("SELECT count(i_id) as total FROM joy.InfomationPublish where i_state=1 and i_template=@template;");
                sb.Append("select top " + pagesize + " * from joy.InfomationPublish where i_state=1 and i_template=@template  order by i_recommend desc,i_id desc;");
            }
            else
            {
                sb.Append("SELECT count(i_id) as total FROM joy.InfomationPublish where i_state=1 and i_template=@template;");
                sb.Append("select * from(");
                sb.Append("select *,ROW_NUMBER() OVER (order by i_recommend desc,i_id desc) as rno from  joy.InfomationPublish where i_state=1 and i_template=@template  ");
                sb.Append(") as a where a.rno>@start and a.rno<@end order by i_recommend desc,i_id desc;");
                query.PageNo = pageno;
                query.PageSize = pagesize; ;
            }
            query.setQueryString(sb.ToString());
            query.addParas("@template", template);
            return base.getList(query);
        }

        public IList getlistweb(int pageno, int pagesize, int template)
        {
            MyQuery query = new MyQuery();
            StringBuilder sb = new StringBuilder();
            if (pageno == 1)
            {
                sb.Append("SELECT count(i_id) as total FROM joy.InfomationPublish where i_state=1 and i_template=@template and (getdate()>i_startTime and getdate()<i_endTime);");
                sb.Append("select top " + pagesize + " * from joy.InfomationPublish where i_state=1 and i_template=@template and (getdate()>i_startTime and getdate()<i_endTime)  order by i_recommend desc,i_id desc;");
            }
            else
            {
                sb.Append("SELECT count(i_id) as total FROM joy.InfomationPublish where i_state=1 and i_template=@template and (getdate()>i_startTime and getdate()<i_endTime);");
                sb.Append("select * from(");
                sb.Append("select *,ROW_NUMBER() OVER (order by i_recommend desc,i_id desc) as rno from  joy.InfomationPublish where i_state=1 and i_template=@template and (getdate()>i_startTime and getdate()<i_endTime) ");
                sb.Append(") as a where a.rno>@start and a.rno<@end  order by i_recommend desc,i_id desc;");
                query.PageNo = pageno;
                query.PageSize = pagesize; ;
            }
            query.setQueryString(sb.ToString());
            query.addParas("@template", template);
            return base.getList(query);
        }

        public IList getinfomationbyid(int id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("select * from joy.InfomationPublish where i_id=@id and i_state=1");
            query.addParas("@id", id);
            return base.getList(query);
        }

        public int delete(int id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("update joy.InfomationPublish set i_state=0 WHERE i_id =@id");
            query.addParas("@id", id);
            return base.exeUpdateSql(query);
        }
    }
}
